package com.happy.study.servier.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.happy.study.dao.RecordMapper;
import com.happy.study.entity.RecordEntity;
import com.happy.study.servier.IRecordService;
import com.happy.study.util.StateEnum;
import com.happy.study.util.StateException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class RecordServiceImpl extends ServiceImpl<RecordMapper, RecordEntity> implements IRecordService {

    @Resource
    private RecordMapper recordMapper;


    @Override
    public List<RecordEntity> queryList ( RecordEntity recordEntity ) {
        //QueryWrapper<RecordEntity> queryWrapper = new QueryWrapper<> ();
        if ( recordEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
//        if ( recordEntity.getUserId () != null ) {
//            queryWrapper.eq ( "userId", recordEntity.getUserId () );
//        }
//        if ( recordEntity.getPaperId () != null ) {
//            queryWrapper.eq ( "paperId", recordEntity.getPaperId () );
//        }
        List<RecordEntity> recordEntities = recordMapper.queryList ( recordEntity.getUserId () );
        return recordEntities;
    }


    @Override
    public RecordEntity querySingle ( RecordEntity recordEntity ) {
        if ( recordEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        recordEntity = baseMapper.selectById ( recordEntity );
        return recordEntity;
    }

    @Override
    public RecordEntity addSingle ( RecordEntity recordEntity ) {
        if ( recordEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        if ( recordEntity.getUserId () == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        recordEntity.setCreateTime ( new Date () );
        int insert = baseMapper.insert ( recordEntity );
        if ( insert == 0 ) {
            throw new StateException ( StateEnum.INSERT_EXCE );
        }
        return recordEntity;
    }
}
